﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Oracle.DataAccess;
using Oracle.DataAccess.Client;
using System.Data;
namespace HY_DbSqlHelp
{
    public class DBsqlHelp
    {
        private static  OracleDataAdapter da;
        private static  OracleDataReader sdr;
        private static  OracleConnection StrConn;
        private static  OracleCommand cmd;
        private static DataSet ds;
        //数据库连接语句
        private static string sqlcon = ConfigurationManager.AppSettings["ConnString"].ToString();
       
        public DBsqlHelp()
        {
            //构造函数
            StrConn = new OracleConnection(sqlcon);
        }

        #region 数据库连接

        /// <summary>
        /// 数据库连接
        /// </summary>
        /// <returns>返回</returns>
        public OracleConnection GetConn()
        {
            if (StrConn.State == ConnectionState.Closed)
            {
                StrConn.Open();
            }
            else if (StrConn.State == ConnectionState.Open)
            {
                StrConn.Close();
            }

            return StrConn;
        }

        #endregion


        /// <summary>
        /// 该方法传入SQL查询语句返回DataTable
        /// </summary>
        /// <param name="sql">要执行的SQL</param>
        /// <returns></returns>
        public DataTable ExecuteQuery(string sql)
        {
            DataTable dt = new DataTable();
            try
            {
                cmd = new OracleCommand(sql, GetConn());
                using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    dt.Load(sdr);
                }
                return dt;
            }
            catch (Exception ex)
            {
                dt = null;  
            }
            return dt;
        }
    }
}
